<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>自定义表单列表</title>
    <#include "/pubHeadStyleTree.html">
</head>
<body>
<title>自定义表单</title>
<div style="position: relative;height: 100%;">
    <div class="ccpui-trees">
        <p class="isShowTree colorSwitch">
            <span style="float: left;color: white">&nbsp;&nbsp;表单分类管理</span>
            <i layadmin-event="treeFlexible" class="layui-icon layui-icon-prev"></i>
        </p>
        <div style="margin-top: 6px;">
            <ul class="ccpui-iconTree iconTrees">
                <li><a class="iconfont iconshuaxin- zTree-refresh" title="刷新"></a></li>
                <li><a class="iconfont iconwenjianjia1-copy zTree-triangle-d" title="展开"></a></li>
                <li><a class="iconfont iconfolder1 zTree-triangle-r" title="收起"></a></li>
                <input type="text" style="width: 50%" class="classZtreeValue">
            </ul>
            <ul id="treeClass" class="ztree ccpui-listZtree"></ul>
        </div>
    </div>
    <div class="ccpui-coreContent formView">
        <table id="customForm" lay-filter="customForm"></table>
    </div>
</div>
<script type="text/html" id="customHeaderTool">
    <div class="ccpui-enterSel">
        <div class="layui-btn-container">
            <a class="layui-btn layui-btn-sm" lay-event="query">
                <i class="layui-icon layui-icon-search"></i>
                <span>查询</span>
            </a>
            <a class="layui-btn layui-btn-sm" lay-event="add">
                <i class="layui-icon layui-icon-add-1"></i>
                <span>添加</span>
            </a>
            <a class="layui-btn layui-btn-sm" lay-event="del">
                <i class="layui-icon layui-icon-delete"></i>
                <span>删除</span>
            </a>
            <a class="layui-btn layui-btn-sm" lay-event="export">
                <i class="layui-icon layui-icon-export"></i>
                <span>导出</span>
            </a>
            <a class="layui-btn layui-btn-sm" lay-event="import">
                <i class="layui-icon layui-icon-download-circle"></i>
                <span>导入</span>
            </a>
            <a class="layui-btn layui-btn-sm" lay-event="setClass">
                <i class="layui-icon layui-icon-set-sm"></i>
                <span>设置分类</span>
            </a>
            <a class="layui-btn layui-btn-sm" lay-event="initTemplate">
                <i class="icon iconfont iconshuaxin2"></i>
                <span>初始化业务数据模板</span>
            </a>
            <a class="layui-btn layui-btn-sm" lay-event="reset">
                <i class="layui-icon layui-icon-refresh"></i>
                <span>重置</span>
            </a>
        </div>
        <form class="layui-form" action="" style="margin-top:10px" lay-filter="formDef">
            <div class="table-top-filter">
                <div class="table-filter-alone">
                    <label class="layui-form-label">表单标题</label>
                    <input type="text" name="Q_subject_SL" id="subject" class="layui-input filter-value"
                           placeholder="表单标题"
                           autocomplete="off"/>
                </div>
                <div class="table-filter-alone">
                    <label class="layui-form-label">表单别名</label>
                    <input type="text" name="Q_formKey_SL" id="formKey" class="layui-input filter-value"
                           placeholder="表单别名"
                           autocomplete="off"/>
                </div>
                <div class="table-filter-alone">
                    <label class="layui-form-label">对应表</label>
                    <input type="text" name="Q_tableName_SL" id="tableName" class="layui-input filter-value"
                           placeholder="对应表"
                           autocomplete="off"/>
                </div>
                <div class="table-filter-alone">
                    <label class="layui-form-label">设计类型</label>
                    <select name="Q_designType_S" id="designType" class="filter-value">
                        <option value="">全部</option>
                        <option value="1">编辑器设计</option>
                        <option value="0">通过表生成</option>
                    </select>
                </div>
                <div class="table-filter-alone">
                    <label class="layui-form-label">发布状态</label>
                    <select name="Q_isPublished_S" id="isPublished" class="filter-value">
                        <option value="">全部</option>
                        <option value="1">已发布</option>
                        <option value="0">未发布</option>
                    </select>
                </div>
            </div>
        </form>
    </div>
</script>
<div class="formDefRightMenus">
    <div id="classRightMenus">
        <ul>
            <li id="addNode">增加节点</li>
            <li id="editNode">修改节点</li>
            <li id="removeNode">删除节点</li>
        </ul>
    </div>
</div>

<script type="text/html" id="operation">
    {{# if(d.isPublished == 1){ }}
    <a class="layui-btn layui-btn-xs" lay-event="newVersion">新建版本</a>
    {{# }}}
    {{# if(d.isPublished == 0){ }}
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    {{# }}}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-xs" lay-event="detailed">明细</a>
    {{# if(d.isPublished == 0){ }}
    <a class="layui-btn layui-btn-xs" lay-event="publish">发布</a>
    {{# }}}
    <a class="layui-btn layui-btn-xs" lay-event="preview">预览</a>
    <a class="layui-btn layui-btn-xs" lay-event="formPermission">表单权限</a>
    {{# if(d.isPublished == 1){ }}
    <a class="layui-btn layui-btn-xs" lay-event="copy">复制</a>
    {{# }}}
    {{# if(d.isPublished == 1){ }}
    <a class="layui-btn layui-btn-xs" lay-event="printTemplate">打印模板</a>
    {{# }}}
    {{# if(d.isPublished == 1){ }}
    <a class="layui-btn layui-btn-xs" lay-event="dataTemplate">业务数据模板</a>
    {{# }}}
    {{# if(d.isPublished == 1){ }}
    <a class="layui-btn layui-btn-xs" lay-event="treeStructure">树结构设置</a>
    {{# }}}
    {{# if(d.isPublished == 1){ }}
    <a class="layui-btn layui-btn-xs" lay-event="businessDataKeep">业务保存设置</a>
    {{# }}}
</script>
<script type="text/html" id="version">
    默认<a lay-event="detailed" style="color: #1E9FFF;cursor: pointer">版本</a>
    <a lay-event="otherVersion" style="color: #1E9FFF;cursor: pointer">更多版本({{d.versionCount}})</a>
</script>

<script>
    layui.config({
        base: serverPath.formPath + staticPath.formPath //静态资源所在路径
    }).extend({
        index: 'index' //主入口模块
    }).use(['index', 'table', 'layer', 'jquery', 'form', 'tree', 'commons', 'common', 'totalFilter', 'setter'], function () {
        var table = layui.table, layer = layui.layer, setter = layui.setter, commons = layui.commons,
            common = layui.common,
            totalFilter = layui.totalFilter, form = layui.form;
        //刷新
        $(".zTree-refresh").click(function () {
            resZtrees();
        });
        //展开
        $(".zTree-triangle-d").click(function () {
            $.fn.zTree.getZTreeObj("treeClass").expandAll(true);
        });
        //收起
        $(".zTree-triangle-r").click(function () {
            $.fn.zTree.getZTreeObj("treeClass").expandAll(false);
        });
        resZtrees();
        loadTable(serverPath.formPath + setter.formLinkPath + 'formDef/list');

        function resZtrees() {
            $.ajax({
                url: serverPath.formPath + setter.sysLinkPath + "globalType/list",
                type: "post",
                dataType: 'json',
                data: {"catKey": "FORM_TYPE", "hasRoot": 1},
                success: function (data) {
                    var zNodes = data.data;
                    var setting = {
                        view: {
                            showLine: false,
                            nameIsHTML: true, //允许name支持html
                            selectedMulti: false
                        },
                        edit: {
                            enable: false,
                            editNameSelectAll: false
                        },
                        data: {
                            simpleData: {
                                enable: true,
                                idKey: "typeId",
                                pIdKey: "parentId"

                            },
                            key: {
                                name: "typeName"
                            }
                        },

                        callback: {
                            onClick: onClickType,
                            onRightClick: OnRightClickres
                        }
                    };
                    $.fn.zTree.init($('#treeClass'), setting, zNodes);
                    component.fuzzySearch('treeClass', '.classZtreeValue', null, true); //初始化模糊搜索方法
                }
            });
        }

        //右键菜单
        function OnRightClickres(event, treeId, treeNode) {
            if (treeNode && !treeNode.noR) {
                ztreeNode = treeNode;
                selectResId = treeNode.typeId;
                parentIds = treeNode.parentId;
                showRMenus(event.clientX, event.clientY);
            }
        }

        var categoryId = "";
        var categoryName = "";

        function onClickType(event, treeId, treeNode) {
            categoryId = treeNode.typeId;
            categoryName = treeNode.typeName;
            var url = serverPath.formPath + setter.formLinkPath + 'formDef/list?categoryId=' + categoryId;
            if (null == treeNode.parentId) {
                url = serverPath.formPath + setter.formLinkPath + 'formDef/list'
            }
            loadTable(url);
        }

        function showRMenus(x, y) {
            $("#classRightMenus ul").show();
            var h = $('#classRightMenus').height();
            x += 15;
            y += 10;
            if ($(window).height() < y + h) (
                y = y - h
            )
            $("#classRightMenus").css({"top": y + "px", "left": x + "px", "visibility": "visible"});

            $("body").bind("mousedown", onBodyMouseDown);
        }

        function onClickRess(event, treeId, treeNode, clickFlag) {
            selectOrgId = treeNode.orgId;
        }

        function hideRMenu() {
            if ($("#classRightMenus")) $("#classRightMenus").css({"visibility": "hidden"});
            $("body").unbind("mousedown", onBodyMouseDown);
        }

        function onBodyMouseDown(event) {
            if (!(event.target.id == "orgRightMenu" || $(event.target).parents("#classRightMenus").length > 0)) {
                $("#classRightMenus").css({"visibility": "hidden"});
            }
        }

        //增加节点
        $("#addNode").click(function () {
            var isRoot;
            if (ztreeNode.parentId == null) {
                isRoot = 1;//根节点
            } else {
                isRoot = 0;
            }
            layer.open({
                type: 2,
                area: ['440px', '280px'],
                title: ['新增节点'],
                content: serverPath.formPath + setter.sysLinkPath + "globalType/view/addNode" + '?isRoot=' + isRoot + '&parentId=' + selectResId
                , end: function () {
                    resZtrees();
                }
            });
        });
        //修改节点
        $("#editNode").click(function () {
            if (ztreeNode.parentId == null) {
                layer.alert("根节点不能修改");
            } else {
                layer.open({
                    type: 2,
                    area: ['440px', '280px'],
                    title: ['修改节点'],
                    content: serverPath.formPath + setter.sysLinkPath + "globalType/view/addNode" + '?typeId=' + selectResId
                    , end: function () {
                        resZtrees();
                    }
                });
            }
        });
        //删除节点
        $("#removeNode").click(function () {
            if (ztreeNode.parentId == null) {
                layer.alert("根节点不能删除");
            } else {
                layer.confirm('确定删除当前节点吗？', function (index) {
                    layer.close(index);
                    $.ajax({
                        url: serverPath.formPath + setter.sysLinkPath + 'globalType/del',
                        type: "post",
                        dataType: 'json',
                        data: {"typeId": selectResId},
                        success: function (result) {
                            layer.alert(result.msg);
                            var zTree = $.fn.zTree.getZTreeObj("treeClass");
                            var idArr = selectResId.split(",");
                            $.each(idArr, function (i, value) {
                                var node = zTree.getNodeByParam("typeId", value);
                                zTree.removeNode(node);
                            })
                        }
                    });
                });
            }
        });

        function loadTable(url) {
            var tableH = commons.tabHeight();
            table.render({
                elem: '#customForm'
                , url: url
                , title: '自定义表单'
                , height: tableH
                , defaultToolbar: []
                , autoSort: false
                , toolbar: '#customHeaderTool'
                , autoSort: false
                , cols: [[
                    {type: 'checkbox'}
                    , {field: 'subject', title: '表单标题', align: 'center', sort: true}
                    , {field: 'formKey', title: '表单别名', align: 'center', sort: true}
                    , {field: 'tableName', title: '对应表', align: 'center', sort: true}
                    , {field: 'typeName', title: '表单分类', align: 'center', sort: true}
                    , {
                        field: 'isPublished', title: '发布状态', align: 'center', sort: true,
                        templet: function (d) {
                            if (d.isPublished == 1) {
                                return '<span style="color:green;">' + '已发布' + '</span>';
                            }
                            return '<span style="color:red;">' + '未发布' + '</span>';
                        }
                    }
                    , {
                        field: 'dataTemplateCount', title: '数据模板', align: 'center',
                        templet: function (d) {
                            if (d.dataTemplateCount == 0) {
                                return '<span style="color:red;">' + '未添加' + '</span>';
                            }
                            return '<span style="color:green;">' + '已添加' + '</span>';
                        }
                    }
                    , {
                        field: 'designType', title: '设计类型', align: 'center', sort: true,
                        templet: function (d) {
                            if (d.designType == 0) {
                                return '<span style="color:green;">' + '通过表生成' + '</span>';
                            }
                            return '编辑器设计';
                        }
                    }
                    , {title: '版本信息', toolbar: '#version', align: 'center', minWidth: 170}
                    , {fixed: 'right', title: '管理', toolbar: '#operation', width: 150, align: 'center'}
                ]]
                , page: true
                , done: function () {
                    common.tableSelectCon('.ccpui-enterSel .layui-form input', 'a[lay-event=query]', '.ccpui-enterSel')
                    table.resize();
                }
            });
            totalFilter.init('customForm');
        }

        //头工具栏事件
        table.on('toolbar(customForm)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data; //获取选中行数据
            var isNeedReload=true;
            switch (obj.event) {
                //自定义表单导出
                case 'export':
                    var Ids = [];
                    for (var i = 0; i < data.length; i++) {
                        Ids.push(data[i].formDefId);
                    }
                    var formDefIds = Ids.join(',');

                    if (data.length == 0) {
                        layer.confirm('还没有选择,请选择表单!', {
                            btn: ['确定']
                        });
                    } else {
                        layer.open({
                            type: 2,
                            title: "导出自定义表单",
                            shade: 0.3,
                            area: ['453px', '300px'],
                            content: serverPath.formPath + setter.formLinkPath + 'formDef/view/export?formDefIds=' + formDefIds
                        })
                    }
                    break;
                //自定义表导入
                case 'import':
                    layer.open({
                        type: 2,
                        title: "导入自定义表单",
                        shade: 0.3,
                        area: ['700px', '500px'],
                        content: serverPath.formPath + setter.formLinkPath + 'formDef/view/import'
                    });
                    break;
                case 'query':
                    common.tableConditionRender("customForm", "formDef", {where: {}}, false);
                    break;
                case 'add':
                    layer.open({
                        type: 2,
                        title: "添加表单",
                        shade: 0.3,
                        left: 200,
                        area: ['760px', '430px'],
                        content: serverPath.formPath + setter.formLinkPath + "formDef/view/add?categoryId=" + categoryId + "&categoryName=" + categoryName
                        ,cancel: function(index, layero){
                            isNeedReload=false;
                        }
                        , end: function () {
                            if(isNeedReload){
                                table.reload('customForm', {
                                    page: {
                                        curr: 1
                                    }
                                });
                                totalFilter.init('customForm');
                            }
                        }
                    });
                    break;
                case 'reset':
                    common.tableConditionRender("customForm", "formDef", {where: {}}, true);
                    break;
                case 'del':
                    if (data.length == 0) {
                        layer.confirm('请选择记录！', {
                            btn: ['确定'] //按钮
                        }, function (index) {
                            layer.close(index);
                        });
                    } else {
                        layer.confirm('确定要删除选中的数据吗?', function (index) {
                            var ids = [];
                            for (var i = 0; i < data.length; i++) {
                                ids.push(data[i].formKey);
                            }
                            var formKey = ids.join(',');
                            $.ajax({
                                url: serverPath.formPath + setter.formLinkPath + "formDef/delByFormKey",
                                data: {"formKey": formKey},
                                dataType: 'json',
                                success: function (data) {
                                    if (null == data.data) {
                                        layer.alert(data.msg);
                                    } else {
                                        layer.alert(data.data + data.msg);
                                    }
                                    //执行重载
                                    table.reload('customForm', {
                                        page: {
                                            curr: 1
                                        }
                                    });
                                    totalFilter.init('customForm');
                                }
                            })
                        });
                    }
                    break;
                case 'setClass':
                    var keys = [];
                    for (var i = 0; i < data.length; i++) {
                        keys.push(data[i].formKey);
                    }
                    var formKeys = keys.join(',');

                    if (data.length == 0) {
                        layer.confirm('还没有选择,请选择表单!', {
                            btn: ['确定']
                        });
                    } else {
                        layer.open({
                            type: 2,
                            title: "设置分类",
                            shade: 0.3,
                            area: ['400px', '250px'],
                            content: serverPath.formPath + setter.formLinkPath + 'formDef/view/classify?formKeys=' + formKeys
                        })
                    }
                    break;
                case 'initTemplate':
                    layer.confirm("初始化业务数据模板将会导致自定义模板信息丢失，确定初始化吗？", {title: '初始化业务数据模板'}, function () {
                        $.ajax({
                            dataType: 'json',
                            url: serverPath.formPath + setter.formLinkPath + "formTemplate/init",
                            success: function (data) {
                                layer.alert(data.msg);
                            },
                            error: function (XMLHttpRequest) {
                                layer.alert("请求失败！" + XMLHttpRequest.status);
                            }
                        });
                    });
                    break;
            }
        });


        //监听行工具事件
        table.on('tool(customForm)', function (obj) {
            var data = obj.data;
            var formDefId = data.formDefId;
            var formKey = data.formKey;
            var tableId = data.tableId;
            switch (obj.event) {
                case 'newVersion':
                    layer.confirm('确定要新建版本吗?', function (index) {
                        var formDefId = obj.data.formDefId;
                        $.ajax({
                            url: serverPath.formPath + setter.formLinkPath + "formDef/newVersion",
                            data: {"formDefId": formDefId},
                            dataType: 'json',
                            success: function (data) {
                                layer.alert(data.msg);
                                //执行重载
                                table.reload('customForm', {
                                    page: {
                                        curr: 1
                                    }
                                });
                            }
                        })
                    });
                    break;
                case 'edit':
                    window.open(serverPath.formPath + setter.formLinkPath + 'formDef/view/formDesign/formDevise?formDefId=' + formDefId);
                    break;
                case 'preview':
                    window.open(serverPath.formPath + setter.formLinkPath + 'formDef/view/formDesign/formPreview?formDefId=' + formDefId);
                    break;
                case 'formPermission':
                    var body;
                    var iframeWin;
                    layer.open({
                        type: 2,
                        title: "表单授权",
                        shade: 0.3,
                        area: ['1000px', '585px'],
                        content: serverPath.formPath + setter.formLinkPath + "formDef/view/formPermission?formKey=" + data.formKey,
                        success:function(layero, index){
                            //获取iframe的body元素
                            body = layer.getChildFrame('body',index);
                            //得到iframe页的窗口对象
                            iframeWin = window[layero.find('iframe')[0]['name']];
                        },
                        end:function () {
                            if (body.find("input[name=return]").val()!="false"){
                                layui.table.reload('customForm', {
                                    page: {
                                        curr: 1
                                    }
                                });
                                layui.totalFilter.init('customForm');
                            }

                        }
                    });
                    break;
                case 'copy':
                    layer.open({
                        type: 2,
                        title: "复制",
                        shade: 0.3,
                        area: ['650px', '400px'],
                        content: serverPath.formPath + setter.formLinkPath + "formDef/view/copy?formDefId=" + data.formDefId
                    });
                    break;
                case 'detailed':
                    layer.open({
                        type: 2,
                        title: "明细",
                        shade: 0.3,
                        area: ['950px', '585px'],
                        content: serverPath.formPath + setter.formLinkPath + "formDef/view/detailed?formDefId=" + data.formDefId
                    });
                    break;
                case 'publish':
                    layer.confirm('确定要发布吗?', function (index) {
                        var formDefId = obj.data.formDefId;
                        $.ajax({
                            url: serverPath.formPath + setter.formLinkPath + "formDef/publish",
                            data: {"formDefId": formDefId},
                            dataType: 'json',
                            success: function (data) {
                                layer.alert(data.msg);
                                //执行重载
                                table.reload('customForm', {
                                    page: {
                                        curr: 1
                                    }
                                });
                                totalFilter.init('customForm');
                            }
                        })
                    });
                    break;
                //更多版本
                case 'otherVersion':
                    var body;
                    var iframeWin;
                    layer.open({
                        type: 2,
                        title: "更多版本",
                        shade: 0.3,
                        area: ['1000px', '585px'],
                        content: serverPath.formPath + setter.formLinkPath + 'formDef/view/versions?formKey=' + formKey,
                        success:function(layero, index){
                            //获取iframe的body元素
                            body = layer.getChildFrame('body',index);
                            //得到iframe页的窗口对象
                            iframeWin = window[layero.find('iframe')[0]['name']];
                        },
                        end:function () {
                            if (body.find("input[name=return]").val()!="false"){
                                layui.table.reload('customForm', {
                                    page: {
                                        curr: 1
                                    }
                                });
                                layui.totalFilter.init('customForm');
                            }
                        }
                    });
                    break;
                case 'del':
                    layer.confirm('确定要删除该表单吗?', function (index) {
                        $.ajax({
                            url: serverPath.formPath + setter.formLinkPath + "formDef/delByFormKey",
                            dataType: 'json',
                            data: {"formKey": formKey},
                            success: function (data) {
                                if (null == data.data) {
                                    layer.alert(data.msg);
                                } else {
                                    layer.alert(data.data + data.msg);
                                }
                                table.reload('customForm', {page: {curr: 1}});
                                totalFilter.init('customForm');
                            }
                        })
                    });
                    break;
                //树结构设置
                case 'treeStructure':
                    layer.open({
                        type: 2,
                        title: "树结构设置",
                        shade: 0.3,
                        area: ['600px', '400px'],
                        content: serverPath.formPath + setter.formLinkPath + 'formDef/view/treeStructure?formKey=' + formKey
                    });
                    break;
                case 'printTemplate':
                    layer.open({
                        type: 2,
                        title: "打印模板",
                        shade: 0.3,
                        area: ['900px', '600px'],
                        content: serverPath.formPath + setter.formLinkPath + 'formDef/view/printTemplate?formKey=' + formKey
                    });
                    break;
                case 'dataTemplate':
                    layer.open({
                        type: 2,
                        title: "业务数据模板",
                        shade: 0.3,
                        maxmin: true,
                        area: ['1090px', '620px'],
                        content: serverPath.formPath + setter.formLinkPath + 'formDef/view/dataTemplate?tableId=' + tableId + "&formKey=" + formKey + "&formDefId=" + formDefId
                    });
                    break;
                case 'businessDataKeep':
                    layer.open({
                        type: 2,
                        title: "业务数据保存设置",
                        shade: 0.3,
                        area: ['1000px', '620px'],
                        content: serverPath.formPath + setter.formLinkPath + 'formDef/view/businessDataKeep?formKey=' + formKey
                    });
                    break;
            }
        });

        common.tableSort("customForm");
    });
</script>
<style>
    .formDefRightMenus #classRightMenus {
        position: fixed;
        border: 1px solid #ccc;
        z-index: 900;
        visibility: hidden;
        top: 0;
        background-color: #f5f5f5;
        text-align: left;
        padding: 2px;
    }

    .formDefRightMenus #classRightMenus ul li {
        margin: 5px 0;
        padding: 0 8px;
        cursor: pointer;
        list-style: none outside none;
    }
</style>
</body>
</html>
<link rel="stylesheet" href="${ctx}/css/font/iconfontie.css" media="all"/>
<link rel="stylesheet" href="${ctx}/iconfont/iconfont.css"/>